package cn.wzvtc.soft14;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.*;

/**
 * Created by linji on 2016/3/4.
 */
@RestController
@EnableAutoConfiguration
@ComponentScan
@CrossOrigin(origins = {},methods = {RequestMethod.GET,RequestMethod.POST,RequestMethod.OPTIONS})

@SpringBootApplication
public class Test extends WebMvcConfigurerAdapter {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Test.class, args);
    }


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/test/**").addResourceLocations("file:///Users/liutim/backendworkspace/ke-end/ext-resources/")
                .setCachePeriod(0);
    }
    @Autowired
    KebiaoService kebiaoService;//new KebiaoService();

    /**
     * 根据周次和班级查找课表信息
     * 测试例子：
     * http://localhost:8080/getlessions?week=0&clazz=0
     * http://localhost:8080/getlessions?week=1&clazz=1
     * @param week -周次
     * @param clazz 课表
     * @return 反回一个符合条件的list，但是没关系，spring会把他们转成json数据返回给客户端的。
     */
    @RequestMapping("/getlessions")
    Object getkebiaobyweekclass(int week,int clazz) {
        return this.kebiaoService.getKebiao(week,clazz);
    }

    /**
     * 获取所有课程信息
     * @return
     */
    @RequestMapping("/getalllessions")
    Object getalllessions() {
        return this.kebiaoService.getAllLession();
    }



    //@RequestMapping("/getlesson")

    @RequestMapping(value="/getlesson",produces = "application/json; charset=UTF-8")
    String getlesson(){
        return "[{\"name\":\"动态编程\"},{\"name\":\"数据分析\"},{\"name\":\"移动应用\"}]";
    }


    /**
     * 添加课程信息，
     * 暂时做到用浏览器访问http://lcalhost:8080/addlession 后，触发本方法，插入一条记录，
     * 但并未让用户在浏览器指定各参数。
     * @return
     */
    @RequestMapping("/addlession")
    void addlession() {
        Ke k=new Ke();
        //一定要指定id，否则数据库会报错。此处设置的uuid可以保证永远唯一
        k.setId(UUID.randomUUID().toString());
        k.setName("三层架构");
        k.setStart(3);
        k.setEnd(5);
        this.kebiaoService.saveKe(k);
    }

}
